﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace Proxy
{
    public class RunEmptyHandle
    {
        SearchInfoHandle Handle_Search;
        /// <summary>
        /// 构造函数
        /// </summary>
        public RunEmptyHandle()
        {
            Handle_Search = new SearchInfoHandle();
        }

        /// <summary>
        /// 清空线体所有板件
        /// Empty all the parts of the line
        /// 
        /// </summary>
        public Task<string> RunEmpty()
        {
            return Task.Factory.StartNew(() => RE());
        }

        public string RE()
        {
            string callBackMsg = string.Empty;
            bool IsFinished = false;
            try
            {
                //入库区域
                //保持进料区域的运行但不允许新工件进入区域直到区域内没有板件

                //出库区域
                //保持出料区域的运行直到区域内没有板件

                //将仓库内所有堆垛全部清空=> 生成出库任务
                do
                {
                LoopStart:
                    Thread.Sleep(500);
                    if (!Globle.isRunEmptyActive)
                    {
                        callBackMsg = "Run Empty was Cancelled";
                        IsFinished = true;
                    }
                    else
                    {
                        //检测当前区域板件数量 等数量为0时 关闭该功能
                        if (IsFinished = Handle_Search.GetTotalBoardsCount() == 0)
                        {
                            callBackMsg = "Run Empty was finished";
                        }
                        else
                        {
                            goto LoopStart;
                        }
                    }
                } while (!IsFinished/*检测当前区域板件数量 等数量为0时 关闭该功能 */);

                return callBackMsg;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_System.Error(ex.Message);
                return ex.Message;
            }
        }
    }
}